import axios from 'axios'

let data = {
  id: '1',
  jsonrpc: '2.0',
  method: 'GetZqHqJbt',
  // params: ['0', 'SZ000002']
  params: []
}
// let url = 'http://192.168.31.210:3001/v2/quote'
let url = 'http://btb-cm.zhangtl.com:8302/v2/quote'

export function getQuote (ID) {
  data.params = test1(ID)
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(res => {
      // console.log('测试---《《《', res)
      if (res.data && res.data.result) {
        let newData = []
        for (let i = 1; i < res.data.result.length; i++) {
          newData.push(test(res.data.result[0], res.data.result[i]))
        }
        resolve(newData)
      } else {
        throw new Error('返回的数据不正确')
      }
    }).catch(err => {
      reject(err)
    })
  })
}

function test (_key, _val) {
  let result = {}
  for (let i = 0; i < _key.length; i++) {
    if (_val) {
      result[_key[i]] = _val[i]
    } else {
      result[_key[i][0]] = _key[i][1]
    }
  }
  return result
}

function test1 (_id) {
  let _arr = _id.split(',')
  let d = ['0']
  let b, c
  for (let i = 0; i < _arr.length; i++) {
    b = _arr[i].split('.')
    c = b[1] + b[0]
    d.push(c)
  }
  return d
}
